/**
 * Structure to switch between canvas and forms:
 *      http://forums.sun.com/thread.jspa?threadID=5265241&tstart=195
 *      Specifically amila325
 */
package Controller;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import UI.UserInterface;

/**
 * Makes GUI and forms, controls the overall program.
 *
 * @author Lewis Marhin and John Gray-Spence
 */
public class Midlet extends MIDlet implements CommandListener {

    UserInterface gui = new UserInterface(this);
    Form fmRename;
    Form fmSave;
    Form fmLoad;
    TextField tfRename;
    TextField tfSave;
    TextField tfLoad;
    ChoiceGroup cgSaveLoc;
    ChoiceGroup cgLoadLoc;
    private Command cmRenameSubmit;
    private Command cmSaveSubmit;
    private Command cmLoadSubmit;
    private Command cmCancel;

    public Midlet() {
        // Midlet() will only run once.
        fmRename = new Form("Rename node");
        fmSave = new Form("Save mindmap");
        fmLoad = new Form("Load mindmap");
        // Create textfields and choice group
        tfRename = new TextField("Rename to: ", "", 25, TextField.ANY);
        tfSave = new TextField("Save as: ", "", 25, TextField.ANY);
        tfLoad = new TextField("File name: ", "", 25, TextField.ANY);
        String[] saveLocations = {"File", "Server", "Local"};
        cgSaveLoc = new ChoiceGroup("Save to: ", Choice.EXCLUSIVE, saveLocations, null);
        cgLoadLoc = new ChoiceGroup("Save to: ", Choice.EXCLUSIVE, saveLocations, null);
        // Create commmands
        cmRenameSubmit = new Command("Rename", Command.OK, 1);
        cmSaveSubmit = new Command("Save", Command.OK, 1);
        cmLoadSubmit = new Command("Load", Command.OK, 1);
        cmCancel = new Command("Cancel", Command.CANCEL, 2);
        // Add text fields and choice group to forms
        fmRename.append(tfRename);
        fmSave.append(tfSave);
        fmSave.append(cgSaveLoc);
        fmLoad.append(tfLoad);
        fmLoad.append(cgLoadLoc);
        // Add softkey functions to form
        fmRename.addCommand(cmRenameSubmit);
        fmRename.addCommand(cmCancel);
        fmSave.addCommand(cmSaveSubmit);
        fmSave.addCommand(cmCancel);
        fmLoad.addCommand(cmLoadSubmit);
        fmLoad.addCommand(cmCancel);
        // Add listeners
        fmRename.setCommandListener(this);
        fmSave.setCommandListener(this);
    }

    /**
     * Runs on startup. Required by Midlet.
     */
    public void startApp() {
        // startApp() will run every time the program is started or unpaused.
        getDisplay().setCurrent(gui);
    }

    /**
     * Runs during a call or when paused. Required by Midlet.
     */
    public void pauseApp() {
    }

    /**
     * Runs when program is called to finish. Required by Midlet.
     *
     * @param unconditional If true, program exits immediately, if false, program finishes current task.
     */
    public void destroyApp(boolean unconditional) {
    }

    /**
     * Gets the current display.
     *
     * @return Display object.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Gets the GUI.
     * 
     * @return The GUI.
     */
    public Displayable getGUI() {
        return gui;
    }

    /**
     * Returns the renaming form with the current name in the text field.
     *
     * @return Rename form.
     */
    public Form getRename() {
        tfRename.setString(gui.getCursorDesc());
        return fmRename;
    }

    /**
     * Returns the form required for saving/sending files.
     *
     * @return Save form.
     */
    public Form getSave() {
        return fmSave;
    }

    /**
     * Returns the form required for loading files.
     *
     * @return Load form.
     */
    public Form getLoad() {
        return fmLoad;
    }

    /**
     * Catches commands thrown by UI. Can initiate file save, load, send and node rename.
     * 
     * @param c Command to be processed.
     * @param d Displayable object.
     */
    public void commandAction(Command c, Displayable d) {
        if (c == cmRenameSubmit) {
            gui.setCursorDesc(tfRename.getString());
            getDisplay().setCurrent(gui);
        } else if (c == cmSaveSubmit) {
            gui.saveFile(tfSave.getString(), cgSaveLoc.getSelectedIndex());
            new Alert("File operation complete.");
            getDisplay().setCurrent(gui);
        } else if (c == cmLoadSubmit) {
            gui.loadFile(tfLoad.getString(), cgSaveLoc.getSelectedIndex());
            new Alert("File operation complete.");
            getDisplay().setCurrent(gui);
        } else if (c == cmCancel) {
            getDisplay().setCurrent(gui);
        }
    }
}
